﻿using System.Runtime.CompilerServices;

namespace Calendar
{
    internal class Program
    {
        /// <summary>
        /// 制作2022年日历
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //输出日历整体框架
            int thisyear = 2022, x = 10, y = 0, varx, vary;        
            for (int i = 1; i < 13; i++)
            {              
                DateTime dt = new DateTime(2022, i, 1);
                int dayinmonth = System.DateTime.DaysInMonth(thisyear, i);
                var dayofweek = dt.DayOfWeek;
                Console.SetCursorPosition(x, y);
                Console.Write("{0}月", i);
                //记录纵坐标
                vary = y;
                //在月份下一行输出星期几
                varx = x - 10;
                Console.SetCursorPosition(varx, ++y);
                Console.Write("日 一 二 三 四 五 六");
                //找到每月1号的位置
                switch (dayofweek)
                {
                    case DayOfWeek.Sunday:
                        x = varx;
                        break;
                    case DayOfWeek.Monday:
                        x = varx + 3;
                        break;
                    case DayOfWeek.Tuesday:
                        x = varx + 6;
                        break;
                    case DayOfWeek.Wednesday:
                        x = varx + 9;
                        break;
                    case DayOfWeek.Thursday:
                        x = varx + 12;
                        break;
                    case DayOfWeek.Friday:
                        x = varx + 15;
                        break;
                    case DayOfWeek.Saturday:
                        x = varx + 18;
                        break;
                    default:
                        break;
                }
                //定位每月1号的位置
                Console.SetCursorPosition(x, ++y);
                for (int j = 1; j < dayinmonth + 1; j++)
                {
                    Console.Write("{0,2} ", j);
                    dt = new DateTime(thisyear, i, j);
                    //到星期六换行
                    if (dt.DayOfWeek.ToString() == "Saturday")
                        Console.SetCursorPosition(varx, ++y);
                }              
                //控制一行三个月的间隔
                y = vary;
                x = varx + 40;
                //每三个月换行
                if (i%3==0)
                {
                    x = 10;
                    y += 10;
                }              
            }
            Console.ReadKey();
        }
    }
}